import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

/**
 * Authors - Oren Bracha, Rony Lahav & Yehuda Eterman
 * This class provides a user interface to the config.txt file (configuration file). It reads the 
 * data from the file, parses it and then is ready to provide it to the user.
 */
public class ConfigFileManager {
	static ConfigFileManager instance;	// Singleton instance
	static String configFileContent;
	
	//singleton CTOR - reads the config file into String
	private ConfigFileManager() {
		try{
			LineNumberReader reader = new LineNumberReader(new FileReader(proj350.configFilePath));
			
			StringBuilder sb = new StringBuilder();
			
			String line = reader.readLine();
			while (line != null) {
				sb.append(line);
				sb.append(MessageCenter.newline);
				line = reader.readLine();
			}
			
			configFileContent = sb.toString();
			configFileContent = configFileContent.toLowerCase();
		}
		catch (IOException e) {
			instance = null;
		}
	}
	
	//singleton
	public static ConfigFileManager getInstance() {
		instance = new ConfigFileManager();
		return instance;
	}
	
	//returns a value from the config String data
	String getConfigVarValue(String field) {
		int firstIndex = configFileContent.indexOf("$"+field+":");
		int middleIndex = configFileContent.indexOf(':', firstIndex);
		int endLineIndex = configFileContent.indexOf("\n", middleIndex);
		
		String strEnvVar = configFileContent.substring(middleIndex+1, endLineIndex);
		if (strEnvVar.endsWith("\r"))
			strEnvVar=strEnvVar.substring(0, strEnvVar.length()-1);
		return strEnvVar;
	}
}
